package com.fzu.express.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzu.express.entity.AddressBook;
import com.fzu.express.entity.Order;
import com.fzu.express.mapper.AddressBookMapper;
import com.fzu.express.service.AddressBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzu.express.utils.ApiException;
import com.fzu.express.utils.CommonUtil;
import com.fzu.express.utils.ErrorEnum;
import com.fzu.express.utils.PageVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyq
 * @since 2022-10-27
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    @Override
    public PageVO listAddressBook(AddressBook addressBook, Integer pageNum, Integer pageSize) {
        // 构造分页参数
        Page<AddressBook> pageRequest = new Page<>(pageNum, pageSize);
        // 构造条件查询器
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        // 结合实际场景，可根据用户userId，name，phone，city,town,detailAddress查询
        // 对条件进行非空判断，不为空时添加查询条件
        // 字符串的非空判断统一使用org.apache.commons.lang3.StringUtils.isNotBlank()
        // 其他类型的非空判断统一使用 != null

        //userId
        Long userId = addressBook.getUserId();
        if (userId != null){
            wrapper.eq(AddressBook::getUserId, userId);
        }
        //name
        String name = addressBook.getName();
        if (StringUtils.isNotBlank(name)){
            wrapper.eq(AddressBook::getName, name);
        }
        //phone
        String phone = addressBook.getPhone();
        if (StringUtils.isNotBlank(phone)){
            wrapper.eq(AddressBook::getPhone, phone);
        }
        //city
        String city = addressBook.getCity();
        if (StringUtils.isNotBlank(city)){
            wrapper.eq(AddressBook::getCity, city);
        }
        //town
        String town = addressBook.getTown();
        if (StringUtils.isNotBlank(town)){
            wrapper.eq(AddressBook::getTown, town);
        }
        //detailAddress
        String detailAddress = addressBook.getDetailAddress();
        if (StringUtils.isNotBlank(detailAddress)){
            wrapper.like(AddressBook::getDetailAddress, detailAddress);
        }
        //进行查询
        IPage<AddressBook> page = super.page(pageRequest, wrapper);
        return PageVO.convert(page);
    }

    @Override
    public void addAddressBook(AddressBook addressBook) {
        String phone = addressBook.getPhone();
        if (!CommonUtil.validPhone(phone)) {
            throw new ApiException(ErrorEnum.PHONE_FORMAT_ERROR);
        }
        super.save(addressBook);
    }

    @Override
    public void updateAddressBook(AddressBook addressBook) {
        // 更新是根据id确定记录的，所以必须对id进行非空判断
        if (addressBook.getId() == null){
            throw new ApiException("ID值不能为空");
        }
        String phone = addressBook.getPhone();
        if (StringUtils.isNotBlank(phone) && !CommonUtil.validPhone(phone)) {
            throw new ApiException(ErrorEnum.PHONE_FORMAT_ERROR);
        }
        super.updateById(addressBook);
    }

    @Override
    public void deleteAddressBook(Long id) {
        super.removeById(id);
    }
}
